package com.ccntec.spark.core.tags.form.input;

import java.util.Map;

import javax.servlet.jsp.JspException;

import com.ccntec.spark.core.BasicTagSupport;

public class SelectMapTag extends BasicTagSupport{
	
	private String name ;
	private String id ;
	private Map<String, String> data ;
	private String selectData;
	private String selectValue;
	private boolean showDefault;
	private boolean disabled ;

	
	@Override
	public int doStartTag() throws JspException {
		StringBuffer html = new StringBuffer();
		html.append("<div class=\"form-group\">");
		html.append("<select class=\"form-control\" name=\""+name+"\" ");
		if(!isNull(id)){
			html.append("id=\""+id+"\" ");
		}
		html.append(">");
		if(showDefault){
			html.append("<option value=\"\">请选择...</option>");
		}
		for(Map.Entry<String, String> map : data.entrySet()){
			String key = map.getKey();
			String value = map.getValue();
			//选项值优先
			if(!isNull(selectData)){
				if(selectData.equals(key)){
					html.append("<option value=\""+value+"\" selected=\"selected\">"+key+"</option>");
				}else{
					html.append("<option value=\""+value+"\">"+key+"</option>");
				}
			}else if(!isNull(selectValue)){
				if(selectValue.equals(value)){
					html.append("<option value=\""+value+"\" selected=\"selected\">"+key+"</option>");
				}else{
					html.append("<option value=\""+value+"\">"+key+"</option>");
				}
			}else{
				html.append("<option value=\""+value+"\">"+key+"</option>");
			}
		}
		html.append("</select>");
		outPrint(html);
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return EVAL_PAGE;
	}
	
	@Override
	public void release() {
		// TODO Auto-generated method stub
		super.release();
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setSelectData(String selectData) {
		this.selectData = selectData;
	}
	public void setSelectValue(String selectValue) {
		this.selectValue = selectValue;
	}
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
	public void setShowDefault(boolean showDefault) {
		this.showDefault = showDefault;
	}
	public void setData(Map<String, String> data) {
		this.data = data;
	}
	
}
